package ICM.client;

import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import common.CommandMessage;
import common.TextMessage;
import common.User;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EmployeesInfo extends JPanel implements Observer {
	
	private ClientUI ui;
	private Client c;
	private User[] users = null;
	private JTable table;
	private JScrollPane pane;
	private DefaultTableModel defaultModel = null;
	private Vector newRow = null;
	
	public EmployeesInfo(final Client c, final ClientUI ui)
	{
		this.c = c;
		this.ui = ui;
		c.addObserver(this);
		setLayout(null);
		
		CommandMessage msg = new CommandMessage("Get Users",null);
		try {
			c.sendToServer(msg);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
		    Thread.sleep(400);
		} catch(InterruptedException ex) {
		    Thread.currentThread().interrupt();
		}
		
		int i;
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "FirstName", "LastName", "Role", "Email", "Department"
			}
		));
		defaultModel = (DefaultTableModel) table.getModel();
        if (users!=null)
		{
			for (i=0;i<users.length;i++)
			{
				newRow = new Vector();
				newRow.add(users[i].getId());
		        newRow.add(users[i].getFirstname());
		        newRow.add(users[i].getLastname());
		        newRow.add(users[i].getRole());
		        newRow.add(users[i].getEmail());
		        newRow.add(users[i].getDepartment());
		        defaultModel.addRow(newRow); 
			}
		}
		JScrollPane pane = new JScrollPane(table); 
		pane.setBounds(31, 72, 867, 226);
		add(pane);
		
		JLabel lblEmployeesInfo = new JLabel("Employees info");
		lblEmployeesInfo.setFont(new Font("Tahoma", Font.PLAIN, 26));
		lblEmployeesInfo.setBounds(204, 11, 218, 37);
		add(lblEmployeesInfo);
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					ui.SetContent(ClientUI.Report_Screen);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} 
			}
		});
		btnBack.setIcon(new ImageIcon(EmployeesInfo.class.getResource("/client/images/back.png")));
		btnBack.setBounds(818, 452, 114, 37);
		add(btnBack);
	}
	
	/**
	 * Handle massage from Observable class in our case Client
	 */
	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
				String msg = "";
				String cmd = "";
				if (arg instanceof TextMessage)
					msg = ((TextMessage)arg).ToDebugMessage();
				else if (arg instanceof CommandMessage) {
					CommandMessage ms=(CommandMessage)arg;
					if(ms.getCommand().equals("All Users"))
							users = (User[]) ms.getParams()[1];
				}
				else
					msg = arg.toString();
	}
}
